package bb.lanxing.lib.devices.base;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import bb.lanxing.lib.devices.antplus.AntPlusDataDispatcher;
import bb.lanxing.lib.devices.api.PeerDevice;
import bb.lanxing.lib.devices.api.SmartDevice;
import bb.lanxing.lib.devices.bici.BiciController;
import bb.lanxing.lib.devices.ble.ble.interfaces.BodySensorLocationController;
import bb.lanxing.lib.devices.ble.ble.interfaces.SensorLocationController;
import bb.lanxing.lib.devices.common.DeviceLogger;
import bb.lanxing.lib.devices.core.utils.DeviceHelper;
import bb.lanxing.lib.devices.utils.IHeartRateAlert;
import bb.lanxing.model.database.Device;
import bb.lanxing.util.Log;

public class LocalDeviceManager extends AbstractXZDeviceManager {
    static final String TAG = "LocalDeviceManager";
    static LocalDeviceManager INSTANCE = null;
    private int device;
    private IHeartRateAlert mHeartRateAlert;

    @Override
    public SensorLocationController getSensorLocationController(String str) {
        return null;
    }

    @Override
    protected boolean isSupportedDevice(int vDeviceType) {
        boolean vRet;
        switch (vDeviceType) {
            case Device.TYPE_BICI, Device.TYPE_CADENCE, Device.TYPE_HEARTRATE,
                 Device.TYPE_X1, Device.TYPE_Q1, Device.TYPE_HEARTRATE_BELT, Device.TYPE_VORTEX_CADENCE, Device.TYPE_CADENCE01, Device.TYPE_HEART_RATE01 -> vRet = true;
            default -> vRet = false;
        }
        Log.d(TAG, "isSupportedDevice: vDeviceType=" + vDeviceType + ", vRet=" + vRet);
        return vRet;
//        return i == 2 || i == 19 || i == 6 || i == 3 || i == 16 || i == 5 || i == 1;
    }

    @Override
    protected boolean shouldPublishDeviceStatus() {
        return true;
    }

    private LocalDeviceManager() {
        Log.d(TAG, "LocalDeviceManager: initial manager");
    }

    public static LocalDeviceManager getInstance() {
        return INSTANCE;
    }

    public static void init() {
        synchronized (LocalDeviceManager.class) {
            if (INSTANCE != null) {
                return;
            }
            LocalDeviceManager localDeviceManager = new LocalDeviceManager();
            INSTANCE = localDeviceManager;
            localDeviceManager.registerActionReceiver();
        }
    }

    public static void terminate() {
        synchronized (LocalDeviceManager.class) {
            LocalDeviceManager localDeviceManager = INSTANCE;
            if (localDeviceManager != null) {
                localDeviceManager.release();
            }
        }
    }

    @Override
    public Context getContext() {
        if (!DeviceContext.isReleased()) {
            return DeviceContext.getApplicationContext();
        }
        return null;
    }

    @Override
    public void onConnectionStateChanged(SmartDevice smartDevice, int newState, int errorCode) {
        this.device = smartDevice.getType();
        processForHeartrateAlert(smartDevice, newState);
        super.onConnectionStateChanged(smartDevice, newState, errorCode);
    }

    private void processForHeartrateAlert(SmartDevice smartDevice, int deviceState) {
        int type = smartDevice.getType();
        if (type == Device.TYPE_HEARTRATE) {
            if (deviceState == STATE_CONNECTED) {
                if (DeviceContext.getDeviceConfiguration().isHeartAlertEnable()) {
                    initHeartAlert();
                }
            } else if (deviceState == STATE_DISCONNECTED) {
                releaseHeartAlert();
            }
        } else if (type == Device.TYPE_HEARTRATE_BELT) {
            if (deviceState == STATE_CONNECTED) {
                if (DeviceContext.getDeviceConfiguration().isHeartAlertEnable()) {
                    initHeartAlert();
                }
            } else if (deviceState == STATE_DISCONNECTED) {
                releaseHeartAlert();
            }
        }
    }

    private void initHeartAlert() {
        IHeartRateAlert heartrateAlert = DeviceContext.getDeviceConfiguration().getHeartrateAlert();
        this.mHeartRateAlert = heartrateAlert;
        if (heartrateAlert != null) {
            BleDataDispatcher.getInstance().addConverter(this.device, this.mHeartRateAlert);
            AntPlusDataDispatcher.getInstance().registerAntPlusDataListener(this.mHeartRateAlert);
        }
    }

    private void releaseHeartAlert() {
        if (this.mHeartRateAlert != null) {
            BleDataDispatcher.getInstance().removeConverter(this.device, this.mHeartRateAlert);
            AntPlusDataDispatcher.getInstance().ungisterAntPlusDataListener(this.mHeartRateAlert);
            this.mHeartRateAlert.release();
            this.mHeartRateAlert = null;
        }
    }

    @Override
    public void onConfigHeartRate(boolean z, int i) {
        super.onConfigHeartRate(z, i);
        if (z && (isConnected(Device.TYPE_HEARTRATE) || isConnected(Device.TYPE_HEARTRATE_BELT))) {
            if (this.mHeartRateAlert == null) {
                initHeartAlert();
            }
            if (mHeartRateAlert != null) {
                mHeartRateAlert.setEnabled(true);
                this.mHeartRateAlert.setAlertValue(i);
            }
        } else {
            if (mHeartRateAlert != null) {
                mHeartRateAlert.setEnabled(false);
                this.mHeartRateAlert.setAlertValue(i);
                releaseHeartAlert();
            }
        }
    }

    @Override
    public BiciController getBiciController(String str) {
        //hu, bici not supported
        return null;
    }

    @Override
    public BodySensorLocationController getBodySensorLocationController(String str) {
        if (str == null) {
            return null;
        }
        PeerDevice deviceByAddress = getDeviceByAddress(str);
        if (deviceByAddress == null) {
            SmartDevice create = DeviceHelper.getDeviceProvider().create();
            create.setAddress(str);
            create.setType(Device.TYPE_VORTEX_CADENCE);
            create.setProtocol(Device.PROTOCOL_BLE);
            deviceByAddress = createDeviceIfNeed(create);
        }
        if (deviceByAddress instanceof BodySensorLocationController) {
            return (BodySensorLocationController) deviceByAddress;
        }
        return null;
    }

    @Override
    public boolean handleDeviceAction(Intent intent) {
        super.handleDeviceAction(intent);
        String action = intent.getAction();

        if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
            intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
        }
        return super.handleDeviceAction(intent);
    }

    @Override
    public void release() {
        super.release();
        Log.d(TAG, "release manager");
        synchronized (LocalDeviceManager.class) {
            INSTANCE = null;
        }
    }

//    @Override
//    protected void d(String str) {
//        DeviceLogger deviceLogger = DeviceContext.getDeviceLogger();
//        if (deviceLogger != null) {
//            deviceLogger.d(TAG, str);
//        }
//    }
}
